<template>
	<view class="login">
		<button type="primary" @tap="getUserProfile" data-eventsync="true">获取用户信息</button>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'

	const {
		proxy
	} = getCurrentInstance()

	const getUserProfile = () => {
		uni.getUserProfile({
			success(user) {
				// console.log("getUserProfile 调用成功：", user);
				uni.login({
					success: async (res) => {
						const data = await proxy.$http("api/user/xcxLogin", {
							code: res.code,
							avatar: user.userInfo.avatarUrl,
							nickname: user.userInfo.nickName,
						})
						if (data.code == 200) {
							uni.setStorageSync("token", data.result.token);
							if (data.code == 200) {
								uni.showToast({
									title: "登录成功",
									icon: 'none'
								})
								setTimeout(() => {
									uni.reLaunch({
										url: "/pages/index/index"
									});
								}, 1000)
							}
						}
					}
				});
			},
			fail(err) {
				console.log(err);
			}
		})
	}
</script>

<style lang="scss" scoped></style>